//
//  ZQComposeView.swift
//  ZQWeibo
//
//  Created by summer的Dad on 16/11/19.
//  Copyright © 2016年 summer的Dad. All rights reserved.
//

import UIKit
import pop
class ZQComposeView: UIView {
    
    var buttomArr : [ZQComposeButton] = [ZQComposeButton]()
    override init(frame: CGRect) {
        
    
        
        super.init(frame: UIScreen.main.bounds)
      
    
       setupUI()
        
        
    }
    
    //添加动画
    func showAnimation(fromVC : UIViewController){
        
        fromVC.view.addSubview(self)
        
        for (index , btn) in buttomArr.enumerated() {
            
        self.animationStar(index: index, btn: btn)
            
        
        }
        
        
    }
    
    
    //点击移除
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
     
        
        for (index , btn) in buttomArr.reversed().enumerated() {
            
          self.animationStar(index: index, btn: btn, isShow: false)
            
            
        }
        
        self.removeFromSuperview()
  
    }
    
    
    //执行动画的方法

    
    func animationStar(index : Int , btn : ZQComposeButton , isShow : Bool  = true){
        
        //POP动画
        
        let springAnimation = POPSpringAnimation(propertyNamed: kPOPViewCenter)
        
        springAnimation?.springBounciness = 10.0
        
        springAnimation?.springSpeed = 10.0
        //
        

       //开始时间
        springAnimation?.beginTime = CACurrentMediaTime() + Double(index) * 0.025
        //设置Y的显示位置
        let btnCenterY = btn.center.y + CGFloat(isShow ? -350 : 350)
        //点击触发到哪里
        springAnimation?.toValue = NSValue.init(cgPoint: CGPoint(x: btn.center.x, y: btnCenterY))
        
        btn.pop_add(springAnimation, forKey: nil)
        
        
    }
    
    
    //ITeme大小动画
    func selectorBtnAnimation(btn : ZQComposeButton){
    
           UIView.animate(withDuration: 0.5, animations: {
            //遍历子控件
            for composeBtn  in self.buttomArr{
            //如果相等就修改
                if btn == composeBtn{
                    
                composeBtn.transform = CGAffineTransform.init(scaleX: 2, y: 2)
                    
                    
                }else{
                    
                 composeBtn.transform = CGAffineTransform.init(scaleX: 0.2, y: 0.2)
                   
                    
                }
            
                composeBtn.alpha = 0.01

            }

        }, completion: { (_) -> () in
         
         //bundleName
            //拿到bundleName
           let bundleName = Bundle.main.infoDictionary!["CFBundleName"] as! String
            let className = bundleName + "." + "ZQComposeTableViewController"
            
            //实例化控制器
          guard  let viewcontroller = NSClassFromString(className) as? UIViewController.Type else{
                 return
            }

            //实例控制
            let vc = viewcontroller.init()
            //导航控制器
            let nav =  UINavigationController(rootViewController: vc)
            //modal控制器
            UIApplication.shared.keyWindow?.rootViewController?.present(nav, animated: true, completion: { 
                
                
                self.removeFromSuperview()
                
            })

        })
    

    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

    func setupUI(){
        
        

//        //拿到toolbar 模糊效果
        let tuBar = UIToolbar(frame: UIScreen.main.bounds)
        self.addSubview(imageback)
        
        tuBar.barStyle = .default
        
        
        self.addSubview(tuBar)
 
        
        
         self.addSubview(imageV)
        
        
        imageV.snp.makeConstraints { (make) in
            
            make.centerX.equalTo(self)
            
            make.top.equalTo(self).offset(100)
        }
        
        self.addsetButton()
    }
    //添加下边按钮
    func addsetButton(){
        
        let manger = (ScreenWidth - composeButtomWith * 3) / 4
        
        for i in 0..<6{
            
            let btn = ZQComposeButton()
            
            //多少行多少列
            let col =  i % 3
            
            let row =  i / 3
            
         //计算Btnx Btny
            let btnX =  manger + (composeButtomWith + manger)  * CGFloat(col)
            
            let btnY =   (composeButtomHeight + manger)  * CGFloat(row) + ScreenHeight
         //设置btn tabbar_compose_music
            btn.setImage(#imageLiteral(resourceName: "tabbar_compose_music"), for: .normal)
            btn.setTitle("Music", for: .normal)
         //设置Frem
            btn.frame = CGRect(x: btnX, y: btnY, width: composeButtomWith, height: composeButtomHeight)
            
            self.addSubview(btn)
            
            buttomArr.append(btn)
            
            btn.addTarget(self, action: #selector(selectorBtnAnimation(btn:)), for: .touchUpInside)
            
            
        }
  
    }
    
  
    
    
    
    //截取显示图片
    private lazy var  imageback : UIImageView = UIImageView(image: UIImage.snapShotCurrentWindow())
    

    
    //懒加载
    lazy var imageV : UIImageView = UIImageView(image: #imageLiteral(resourceName: "compose_slogan"))
    
    
    
    
}
   
